/**
 * 所有自定义控件命名控件 
 */
Helper.control = Helper.control ? Helper.control : {};
/**
 * 选项数据对象
 */
Helper.control.Data = function(value,label,index){
	this.value = value;
	this.index = index;
	this.label = label || value;
};
/**
 * 选项框类
 */
Helper.control.SelectContent = function(P){
	var params =  P || {};
	Helper.Components.call(this, P);
	var _aSelectValues = []; // 控件的值列表,该数据存放Helper.control.Data类型数据
	this.renderId = P.renderId;
	this.selCss = P.selCss || "list-Selected";
	this.delCallBack = P.delCallBack;//删除行成功后触发事件
	this.clearCallBack = P.clearCallBack;//清楚数据后触发事件
	this.insertCallBack = P.insertCallBack;//插入数据触发事件
	this.selectRowCallBack = P.selectRowCallBack;//选中行触发事件
	this.updateRowCallBack = P.updateRowCallBack;//修改触发事件
	
	var _sTopStr = "<ul class='betList'>";
	var _sBottomStr = "</ul>";
	var _sLiIdTop = this.renderId+"_SelectContent_id_";
	var THIS = this;
	if (typeof (Helper.control.SelectContent.__initialized) === "undefined") {
		// 刷新控件的值
		Helper.control.SelectContent.prototype.flush = function(){
			var _sContect = _sTopStr;
			for(var i=0;i<_aSelectValues.length;i+=1) {
				_sContect+="<li id='"+(_sLiIdTop+_aSelectValues[i].index.toString())+"'><span class='snum sssq'>"+_aSelectValues[i].label+"</span><a href='javascript:void(0)' class='sdel'>删除</a></li>";
			}
			_sContect += _sBottomStr;
			$("#"+THIS.renderId).html(_sContect);
			$("#"+THIS.renderId+" div ul li a").click(function(){
				var _sIdStr = $(this).parent().attr("id");
				var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
				THIS.remove(_nIndex);
				return false;
			});
			$("#"+this.renderId+" div ul li").click(function(){
				$("#"+THIS.renderId+" div ul li").attr("class","");
				$(this).attr("class",THIS.selCss);
				if(THIS.selectRowCallBack) {
					var _sIdStr = $(this).attr("id");
					
					var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
					THIS.selectRowCallBack(_nIndex);
				}
			});
		};
		// 添加数据
		Helper.control.SelectContent.prototype.add = function(value,label,index) {
			var _oData = new Helper.control.Data(value,label,index);
			_aSelectValues.push(_oData);
			var _update = "";
			var _ptype=Helper.lottery.lotteryData.playtype;
			if(THIS.updateRowCallBack && Helper.lottery.lotteryData && (_ptype===99 || _ptype===5)) {
				_update="<a href='javascript:void(0)' lang='update' class='sdel d_blue mr_10'>修改</a>";
			}
			var _sContent ="<li id='"+(_sLiIdTop+_oData.index.toString())+"'><span class='snum sssq'>"+(_oData.label || _oData.value)+"</span><a href='javascript:void(0)' class='sdel d_blue' lang='del'>删除</a>"+_update+"</li>";
			
			$("#"+this.renderId+" ul").append(_sContent);
			$("#"+(_sLiIdTop+_oData.index.toString())+" a[lang='del']").click(function(){
				var _sIdStr = $(this).parent().attr("id");
				var _nIndex = parseInt(_sIdStr.substr(_sLiIdTop.length),10);
				THIS.remove(_nIndex);
				return false;
			});
			$("#"+(_sLiIdTop+_oData.index.toString())+" a[lang='update']").click(function(){
				if(THIS.updateRowCallBack) {
					THIS.updateRowCallBack(_oData.index);
				}
				return false;
			});
			$("#"+(_sLiIdTop+_oData.index.toString())).click(function(){
				$("#"+THIS.renderId+" ul li").attr("class","");
				$(this).attr("class",THIS.selCss);
				
				if(THIS.selectRowCallBack) {
					THIS.selectRowCallBack(_oData.index);
				}
			});
			if(THIS.insertCallBack) {
				THIS.insertCallBack(_oData);
			}
		};
		// 删除数据
		Helper.control.SelectContent.prototype.remove = function(index){
			for (var i = 0; i < _aSelectValues.length; i += 1) {
				if(_aSelectValues[i].index === index) {
					_aSelectValues.splice(i,1);//移除数据
				}
			}
			$("#"+(_sLiIdTop+index.toString())).remove();
			if(THIS.delCallBack){
				THIS.delCallBack(index);
			}
		};
		// 清空数据
		Helper.control.SelectContent.prototype.clear = function() {
			_aSelectValues = [];
			THIS.flush();//刷新界面
			if(THIS.clearCallBack) {
				THIS.clearCallBack();
			}
		};
		// 获取控件的数据
		Helper.control.SelectContent.prototype.getData = function() {
			var _aTemp = [];
			for(var i=0;i<_aSelectValues.length;i+=1) {
				_aTemp[i] = _aSelectValues[i];
			}
			return _aTemp;
		};
		// 设置标识，已经初始化过原型方法
		Helper.control.SelectContent.__initialized = true;
	}	
};

/**
 * 选项数据对象
 */
Helper.control.SelectData = function(value,label,index,isSelect){
	this.value = value;
	this.label = label || value;
	this.isSelect = p.isSelect === undefined ? false : p.isSelect;
	
	this.index = index;
};

/**
 * 选项数据对象
 */
Helper.control.SelectData = function(value,label,index,isSelect){
	this.value = value;
	this.label = label || value;
	this.isSelect = p.isSelect === undefined ? false : p.isSelect;
	
	this.index = index;
};

// Select下拉框控件
Helper.control.Select1 = function(p){
	var params =  p || {};
	Helper.Components.call(this, p);
	this.renderId = p.renderId;
	this.index = new Helper.Index();
	this.data = null;//空间当前值
	this.onSelect = p.onSelect;//选中后出发的事件
	this.selClass = "fake-select hover";
	this.defaultClass = "fake-select";
	this.selectConectPrefix = "helper_select_";
	this.valueMap = new Helper.Map();
	if(p.values && p.values.length>0) {
		for(var i=0;i<p.values.length;i+=1) {
			this.add(p.values[i].value,p.values[i].label);
			if(p.values[i].isSelect) {
				this.data = p.values[i];
			}
		}
	}
};
Helper.control.Select1.prototype.add = function(value,label) {
	this.valueMap.put(this.index.getIndex(),{value:value,label:label});
};
Helper.control.Select1.prototype.flush = function(){
	 var THIS = this;
	 var _defaultContent = this.data && this.data.label;
	 var _sTop = "<span class='"+this.defaultClass+"'><ul class='selected'><li><s class='sel_dropdown'><s class='i'></s></s> <a href='javascript:void(0);'>"+(_defaultContent || "选择")+"</a></li></ul><ul class='toselect'>";
	 var _sBottom = "</ul></span>";
	 var _sContext = _sTop;
	 var _keys = this.valueMap.keys();
	 for(var i=0;i<_keys.length;i+=1) {
		 var _data = this.valueMap.get(_keys[i]);
		 _sContext+="<li><a name='"+this.selectConectPrefix+_keys[i]+"' href='javascript:void(0);'>"+_data.label+"</a></li>";
	 }
	 _sContext+=_sBottom;
	 $("#"+THIS.renderId).html(_sContext);
	 $("#"+THIS.renderId+">span>ul").mouseover(function(){
		 $("#"+THIS.renderId+">span").attr("class","fake-select hover");
	 });
	 $("#"+THIS.renderId+">span>ul").mouseout(function(){
		 $("#"+THIS.renderId+">span").attr("class","fake-select");
	 });
	 $("#"+THIS.renderId+">span>ul[class='toselect']>li>a").click(function(){
		 var _name = $(this).attr("name");
		 var _index = _name.substr(THIS.selectConectPrefix.length);
		 THIS.data = THIS.valueMap.get(Helper.doms.parseInt(_index, 10));
		 $("#"+THIS.renderId+">span>ul[class='selected']>li>a").html(THIS.data.label);
		 $("#"+THIS.renderId+">span").attr("class","fake-select");
		 if(typeof(THIS.onSelect) === "function") {
			 THIS.onSelect(THIS.data);
		 }
	 });
	 
};
Helper.control.Select1.prototype.getValue = function(){
	if(this.data) {
		return this.data.value;
	} 
};
Helper.control.Select1.prototype.getLabel = function(){
	if(this.data) {
		return this.data.label;
	} 
};

//Select下拉框控件
//[{value : 1,label : 1},{value : 1,label : 1,isSelect : true}]
Helper.control.Select = function(p){
	var params =  p || {};
	Helper.Components.call(this, p);
	this.renderId = p.renderId;
	this.index = new Helper.Index();
	this.data = null;//空间当前值
	this.onSelect = p.onSelect;//选中后出发的事件
	this.onChange = p.onChange;//改变后触发的事件
	this.selClass = "selected";
	this.defaultClass = "";
	this.selectConectPrefix = "helper_select_";
	this.width = p.width;
	this.valueMap = new Helper.Map();
	if(p.values && p.values.length>0) {
		for(var i=0;i<p.values.length;i+=1) {
			this.add(p.values[i].value,p.values[i].label);
			if(p.values[i].isSelect) {
				this.data = p.values[i];
			}
		}
	}
};
Helper.control.Select.prototype.add = function(value,label,isSelect) {
	var _index = this.index.getIndex();
	this.valueMap.put(_index, {index:_index, value:value, label:label});
	if(isSelect) {
		this.data = {index:_index, value:value, label:label};
	}
};
Helper.control.Select.prototype.clear = function(){
	this.valueMap.clear();
	this.data = null;
};
Helper.control.Select.prototype.flush = function(){
	var THIS = this;
	var _defaultContent = this.data && this.data.label;
	var _sTop = "<div class='drop Position_relative'>"
			+ "<div class='default'>"
			+ "<div class='public_bg yj_left l'></div>"
			+ "<div class='public_bg yj_middle l' "+(THIS.width?"style='width:"+THIS.width+"px;'":"")+">"+(_defaultContent || "请选择")+"</div>"
			+ "<div class='l public_bg yj_right'></div>"
			+ "</div>"
			+ "<div class='drop_box Position_absolute'>"
			+ "<ul>";
	var _sBottom = "</ul></div></div>";;
	var _sContext = _sTop;
	var _keys = this.valueMap.keys();
	//临时添加一个标签到页面上，用于计算下拉区域最大的宽度。
	$("body").append("<div id='temp' style='display:none;'></div>");
	var tempWidth = 0;var widthSpace = 10; 
	var tempHeight = 450;
	for(var i=0;i<_keys.length;i+=1) {
		var _data = this.valueMap.get(_keys[i]);
		_sContext+="<li name='"+this.selectConectPrefix+_keys[i]+"'>"+_data.label+"</li>";
		tempWidth = ($("#temp").html(_data.label).width()+widthSpace)>tempWidth?($("#temp").html(_data.label).width()+widthSpace):tempWidth;
	}
	//移除临时添加的标签
	$("#temp").remove();
	
	_sContext+=_sBottom;
	$("#"+THIS.renderId).html(_sContext);
	if(!$("#"+THIS.renderId).hasClass("l")){
		$("#"+THIS.renderId).addClass("l");
	}
	if($("#"+THIS.renderId).width()<tempWidth)
	{//为下拉区域附上宽度
		$("#"+THIS.renderId+">div>div:last").css("width", tempWidth);
	}
	if($("#"+THIS.renderId+">div>div:last").height()>tempHeight)
	{//为下拉区域附上高度
		$("#"+THIS.renderId+">div>div:last").css("height", tempHeight);
		if($("#"+THIS.renderId).width()<tempWidth)
		{//再加上20像素被滚动条遮挡的宽度
			tempWidth += 20;
			$("#"+THIS.renderId+">div>div:last").css("width", tempWidth);
		}
	}
	$("#"+THIS.renderId+">div").hover(function(){
		$("#"+THIS.renderId+">div").addClass(THIS.selClass);
	},function(){
		$("#"+THIS.renderId+">div").removeClass(THIS.selClass);
	});
	$("#"+THIS.renderId+">div>div:last>ul>li").each(function(index){
		var _name = $(this).attr("name");
		var _index = _name.substr(THIS.selectConectPrefix.length);
		$(this).click(function(){
			var _data = THIS.valueMap.get(Helper.doms.parseInt(_index, 10));
			//判断是否下标改变
			if(!THIS.data || THIS.data.index != _data.index)
			{
				THIS.data = _data;
				if($.isFunction(THIS.onChange)) {
					THIS.onChange(THIS.data);
				}
			}
			$("#"+THIS.renderId+">div>div:first>div[class*='yj_middle']").html(THIS.data.label);
			$("#"+THIS.renderId+">div").removeClass(THIS.selClass);
			if($.isFunction(THIS.onSelect)) {
				THIS.onSelect(THIS.data);
			}
			//重置下拉区域宽度
			$("#"+THIS.renderId+">div>div:last").css("width", $("#"+THIS.renderId).width()>tempWidth?$("#"+THIS.renderId).width():tempWidth);
		});
	});
};
Helper.control.Select.prototype.getValue = function(){
	if(this.data) {
		return this.data.value;
	} 
};
Helper.control.Select.prototype.getLabel = function(){
	if(this.data) {
		return this.data.label;
	} 
};
Helper.control.Select.prototype.getDataByValue = function(value){
	var _values = this.valueMap.values();
	for(var i=0;i<_values.length;i+=1) {
		if(value===_values[i].value) {
			return _values[i];
		}
	}
};
Helper.control.Select.prototype.selValue=function(value) {
	var data = this.getDataByValue(value);
	this.data = data;
};


/**
 * 暂时使用，有待进一步封装
 */
Helper.control.SelectDate = function(p){
	p = p || {};
	this.renderId = p.renderId;
	this.onSelect = p.onSelect;
	this.onChange = p.onChange;
	this.spanMonth = p.spanMonth;
	this.valueFormat = p.valueFormat || "yyyyMM";
	this.labelFormat = p.labelFormat || "yyyy年MM月";
	var selectData = new Array();
	/**
	 * 生成日期下拉框数据
	 */
	var THIS = this;
	var dateSelect = new Helper.control.Select({
		renderId : THIS.renderId,
		onSelect : function(data){
			if($.isFunction(THIS.onSelect)) {
				THIS.onSelect(data);
			}
		},
		onChange : function(data){
			if($.isFunction(THIS.onChange)) {
				THIS.onChange(data);
			}
		}
	});
	var _spanMonth= THIS.spanMonth;
	var _valueFormat = THIS.valueFormat;
	var _labelFormat = THIS.labelFormat;
	var nowDate;// = new Date($.ajax({async:false}).getResponseHeader("Date"));
	$.ajax({
		type: "POST",
		async: false,
		url: Helper.basePath + "/getSystemDate.htm",
		success: function(data){
			nowDate = new Date(data.returnObject);
		}
	});
	var year = nowDate.getFullYear();//获取当前年
	var month = nowDate.getMonth()+1;//获取当前月
	for(var i=1;i<=_spanMonth;i++){
		if(month==0){month=12;year--;}
		var tempDate=new Date();tempDate.setFullYear(year,--month,1);
		dateSelect.add(tempDate.format(_valueFormat), tempDate.format(_labelFormat), tempDate.format(_valueFormat) == nowDate.format(_valueFormat));
	}
	return dateSelect;
};